package com.buer.train.api;

import com.buer.train.business.domain.MechanismDO;
import com.buer.train.business.service.MechanismService;
import com.buer.train.common.annotation.Auth;
import com.buer.train.common.eumn.ResultEnum;
import com.buer.train.common.utils.BaseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/mechanism")
@Api(tags = {"培训机构相关接口"})
public class MechanismController {

    @Autowired
    private MechanismService mechanismService;

    @RequestMapping(value = "/mechanism/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("培训机构列表")
    @Auth(value = "mechanism:mechanism:list")
    public BaseResult mechanismList(@RequestBody Map<String, Object> params) {

        PageInfo<MechanismDO> mechanismList = mechanismService.getMechanismList(params);
        Map map = new HashMap<>();
        map.put("list",mechanismList.getList());
        map.put("total",mechanismList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS,map);
    }

    @RequestMapping(value = "/mechanism/getAllMechanism", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取所有培训机构")
    @Auth
    public BaseResult getAllMechanism(@RequestBody Map<String, Object> params) {

        List<MechanismDO> mechanismDOList = mechanismService.list(params);
        return BaseResult.build(ResultEnum.SUCCESS,mechanismDOList);
    }

    @RequestMapping(value = "/mechanism/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("新增培训机构")
    @Auth(value = "mechanism:mechanism:add")
    public BaseResult mechanismAdd(@RequestBody MechanismDO mechanism) {

        mechanism.setCreateTime(new Date());
        mechanism.setUpdateTime(new Date());
        if(mechanismService.save(mechanism)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/mechanism/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除培训机构")
    @Auth(value = "mechanism:mechanism:remove")
    public BaseResult teacherRemove(@RequestBody Map<String, Object> params) {
        if (Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.FAILURE);
        }
        Integer id = Integer.valueOf(params.get("id") + "");

        if(mechanismService.remove(id)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/mechanism/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改培训机构")
    @Auth(value = "mechanism:mechanism:update")
    public BaseResult mechanismUpdate(@RequestBody MechanismDO mechanism) {
        MechanismDO mechanismDO = mechanismService.get(mechanism.getId());
        mechanismDO.setName(mechanism.getName());
        mechanismDO.setAddress(mechanism.getAddress());
        mechanismDO.setOfficialSealImg(mechanism.getOfficialSealImg());
        mechanismDO.setPhone(mechanism.getPhone());
        mechanismDO.setType(mechanism.getType());
        mechanismDO.setUpdateTime(new Date());
        if(mechanismService.update(mechanism)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/mechanism/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID查询培训机构")
    @Auth
    public BaseResult teacherGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        MechanismDO mechanismDO = mechanismService.get(id);

        return BaseResult.build(ResultEnum.SUCCESS,mechanismDO);
    }
}
